package controller

import (
	"context"
	"github.com/gin-gonic/gin"
	"github.com/yyangl/yoyo/application"
	"github.com/yyangl/yoyo/proto"
	"google.golang.org/grpc"
)

type Promises struct {
	application.BaseController
}

func (p *Promises) RegRouter() {
	p.Get("promises", p.Promise)
	p.Get("users/promises", p.UsersPromises)
}

func (p *Promises) Promise(ctx *gin.Context) {

}

func (p *Promises) UsersPromises(ctx *gin.Context) {
	_, addr, err := p.GetApp().GetService("admin")
	if err != nil {
		p.ServiceErr(ctx)
		return
	}
	conn, err := grpc.Dial(addr, grpc.WithInsecure())
	if err != nil {
		p.ServiceErr(ctx)
		return
	}

	cli := proto.NewAdminServiceClient(conn)
	c := context.WithValue(context.Background(), "request-id", ctx.Writer.Header().Get("X-Request-Id"))
	resp, err := cli.PromisesById(c, &proto.PromisesReq{
		Uid: ctx.Keys["uid"].(int64),
	})
	if err != nil {
		p.Err(ctx, err.Error())
	}
	//fmt.Printf("%v", ctx.Keys)
	p.OK(ctx, resp)
}
